package com.xiaoyu.lanling.feature.chat.viewholder.emoji

import `in`.srain.cube.views.list.ViewHolderBase
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.emoji.widget.EmojiTextView
import com.xiaoyu.base.utils.extensions.getViewTag
import com.xiaoyu.base.utils.extensions.setOnClickDebounceListener
import com.xiaoyu.base.utils.extensions.setViewTag
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.event.chat.emoji.ChatEmojiCheckEvent
import com.xiaoyu.lanling.feature.chat.model.emoji.ChatEmojiGridItem

class ChatEmojiGridViewHolder : ViewHolderBase<ChatEmojiGridItem>() {

    private var mEmojiTextView: EmojiTextView? = null

    override fun createView(layoutInflater: LayoutInflater, parent: ViewGroup): View? {
        val view = layoutInflater.inflate(R.layout.item_chat_emoji_grid, parent, false)
        mEmojiTextView = view.findViewById(R.id.chat_emoji)

        mEmojiTextView?.setOnClickDebounceListener(sOnClickListener)

        return view
    }

    override fun showData(position: Int, itemData: ChatEmojiGridItem) {
        mEmojiTextView?.text = itemData.emojiKey
        mEmojiTextView?.setViewTag(itemData)
    }

    companion object {
        private val sOnClickListener = View.OnClickListener { v: View ->
            val itemData: ChatEmojiGridItem = v.getViewTag<ChatEmojiGridItem>()
                    ?: return@OnClickListener
            ChatEmojiCheckEvent.create(itemData).post()
        }
    }
}